package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WmsOutWarehouseOrder;
import com.efast.dmom.mes.dao.entity.WmsOutWarehouseOrderDao;
import com.efast.dmom.mes.dao.entity.WmsPurchaseOrderDao;
import com.efast.dmom.mes.service.WmsOutWarehouseOrderService;
import com.efast.dmom.mes.service.WmsPurchaseOrderHistoryService;
import com.efast.dmom.mes.service.WmsWarehouseStorageService;
import com.efast.dmom.mes.util.ServiceBaseEntityUtil;
import com.efast.dmom.mes.util.WmsOrderLogStatus;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;
@Service
@Transactional
public class WmsOutWarehouseOrderServiceImpl implements WmsOutWarehouseOrderService {

	@Autowired
	private WmsPurchaseOrderHistoryService wmsPurchaseOrderHistoryService;
	@Autowired
	private WmsWarehouseStorageService wmsWarehouseStorageService;
	@Autowired
	private WmsOutWarehouseOrderDao wmsOutWarehouseOrderDao;
	
	@Autowired
	private WmsPurchaseOrderDao wmsPurchaseOrderDao;
	
	@Autowired
	private ServiceBaseEntityUtil serviceBaseEntityUtil;
	
	@Override
	public ResultBean batchSave(List<WmsOutWarehouseOrder> wmsOutWarehouseOrder) {
		ResultBean resultBean = new ResultBean(true);
		try{
			//生成单号
			String no;
			if(wmsOutWarehouseOrder.get(0).getOutWarehouseOrder()!=null){
				no = wmsOutWarehouseOrder.get(0).getOutWarehouseOrder();
				for (WmsOutWarehouseOrder wmsOutWarehouseOrder2 : wmsOutWarehouseOrder) {
					WmsOutWarehouseOrder wmsOutWarehouseOrder1 = new WmsOutWarehouseOrder();
					wmsOutWarehouseOrder1.setOutWarehouseOrder(wmsOutWarehouseOrder2.getOutWarehouseOrder());
					wmsOutWarehouseOrder1.setPartId(wmsOutWarehouseOrder2.getPartId());
					wmsOutWarehouseOrder1.setWarehouseId(wmsOutWarehouseOrder2.getWarehouseId());
					List<WmsOutWarehouseOrder> selectByOutwarehouseNoAndpartId = wmsOutWarehouseOrderDao.selectByOutwarehouseNoAndpartId(wmsOutWarehouseOrder1);
					if (selectByOutwarehouseNoAndpartId.size() >0) {
						resultBean.setErrorMessage("当前仓库下新增物料已存在！");
						return resultBean;
					}
					List<WmsOutWarehouseOrder> selectByOutInwarehouseNo = wmsOutWarehouseOrderDao.selectByOutInwarehouseNo(wmsOutWarehouseOrder1);
					if (selectByOutInwarehouseNo.size()>0) {
						if (!selectByOutInwarehouseNo.get(0).getType().equals(wmsOutWarehouseOrder2.getType())) {
							resultBean.setErrorMessage("入库类型必须为/"+selectByOutInwarehouseNo.get(0).getType()+"/请检查");
							return resultBean;
						}
					}
				}
			}else{
				no = serviceBaseEntityUtil.getNo("CKD_code");
			}
			wmsOutWarehouseOrder.forEach(item -> {
				//插入默认字段及单号
				ServiceBaseEntityUtil.setSaveBaseEntityData(item);
				ServiceBaseEntityUtil.setUpdateBaseEntityData(item);
				item.setOutWarehouseOrder(no);
				item.setOutWarehouseStatus(WmsOrderStatusUtil.NOCONFIRM);
			});
			//校验库存
			String result = wmsWarehouseStorageService.checkStorageEnough(wmsOutWarehouseOrder);
			if(StringUtil.isNotBlank(result)){
				resultBean.setSuccess(false);
				resultBean.setMessage(result);
				return resultBean;
			}
			// 保存出库单信息
			wmsOutWarehouseOrderDao.batchSave(wmsOutWarehouseOrder);
			//保存日志
			wmsPurchaseOrderHistoryService.saveOutWarehouseLog(wmsOutWarehouseOrder, WmsOrderLogStatus.ADD.getOperation());
		}catch(Exception e){
			e.printStackTrace();
			resultBean.setSuccess(false);
			resultBean.setMessage(e.getMessage());
			throw new RuntimeException();
		}
		return resultBean;
	}

	@Override
	public ResultBean update(WmsOutWarehouseOrder wmsOutWarehouseOrder) {
		ResultBean resultBean = new ResultBean(true);
		try{
			// 出库物料是否重复校验
			WmsOutWarehouseOrder wmsOutWarehouseOrder1 = new WmsOutWarehouseOrder();
			wmsOutWarehouseOrder1.setOutWarehouseOrder(wmsOutWarehouseOrder.getOutWarehouseOrder());
			wmsOutWarehouseOrder1.setPartId(wmsOutWarehouseOrder.getPartId());
			wmsOutWarehouseOrder1.setWarehouseId(wmsOutWarehouseOrder.getWarehouseId());
			List<WmsOutWarehouseOrder> selectByOutwarehouseNoAndpartId = wmsOutWarehouseOrderDao.selectByOutwarehouseNoAndpartId(wmsOutWarehouseOrder1);
			if (selectByOutwarehouseNoAndpartId.size() >0 && selectByOutwarehouseNoAndpartId.get(0).getId() != wmsOutWarehouseOrder.getId()) {
				resultBean.setErrorMessage("新增物料已存在！");
				return resultBean;
			}
			//校验出库类型
			List<String> types = wmsOutWarehouseOrderDao.getOrderOutType(wmsOutWarehouseOrder);
			if(types.size() >0){
				resultBean.setSuccess(false);
				resultBean.setMessage("同一出库单出库类型需要保持一致");
				return resultBean;
			}
			
			//校验库存
			String result = wmsWarehouseStorageService.checkStorageEnough(wmsOutWarehouseOrder);
			if(StringUtil.isNotBlank(result)){
				resultBean.setSuccess(false);
				resultBean.setMessage(result);
				return resultBean;
			}
			ServiceBaseEntityUtil.setUpdateBaseEntityData(wmsOutWarehouseOrder);
			// 保存更新数据
			wmsOutWarehouseOrderDao.update(wmsOutWarehouseOrder);
			//保存日志
			wmsPurchaseOrderHistoryService.saveOutWarehouseLog(wmsOutWarehouseOrder, WmsOrderLogStatus.UPDATE.getOperation());
		}catch(Exception e){
			e.printStackTrace();
			resultBean.setSuccess(false);
			resultBean.setMessage(e.getMessage());
		}
		return resultBean;
	}

	@Override
	public ResultBean batchDelete(List<WmsOutWarehouseOrder> list) {
		ResultBean resultBean = new ResultBean(true);
		try{
			for (WmsOutWarehouseOrder wmsOutWarehouseOrder : list) {
				List<WmsOutWarehouseOrder> listdetal = queryAllOrderByNoOrId(wmsOutWarehouseOrder);
				// 批量删除出库信息
				wmsOutWarehouseOrderDao.batchDelete(listdetal);
				//保存日志
				wmsPurchaseOrderHistoryService.saveOutWarehouseLog(listdetal, WmsOrderLogStatus.DELETE.getOperation());
			}
		}catch(Exception e){
			e.printStackTrace();
			resultBean.setSuccess(false);
			resultBean.setMessage(e.getMessage());
		}
		return resultBean;
	}
	@Override
	public ResultBean batchDeleteDetail(List<WmsOutWarehouseOrder> list) {
		ResultBean resultBean = new ResultBean(true);
		try{
			// 批量删除出库信息
			wmsOutWarehouseOrderDao.batchDelete(list);
			//保存日志
			wmsPurchaseOrderHistoryService.saveOutWarehouseLog(list, WmsOrderLogStatus.DELETE.getOperation());
		}catch(Exception e){
			e.printStackTrace();
			resultBean.setSuccess(false);
			resultBean.setMessage(e.getMessage());
		}
		return resultBean;
	}

	@Override
	public ResultBean batchConfirm(List<WmsOutWarehouseOrder> list) throws RuntimeException {
		ResultBean resultBean = new ResultBean(true);
			for (WmsOutWarehouseOrder wmsOutWarehouseOrder : list) {
				List<WmsOutWarehouseOrder> detaliList = queryAllOrderByNoOrId(wmsOutWarehouseOrder);
				detaliList.forEach(item -> {
					item.setUnitPrice(wmsOutWarehouseOrder.getUnitPrice());
					ServiceBaseEntityUtil.setUpdateBaseEntityData(item);
					item.setOutWarehouseStatus(WmsOrderStatusUtil.CONFIRM);
					item.setOperationUser(SpringWebUtils.getRequestUser());
				});
				// 出库单确认信息保存
				wmsOutWarehouseOrderDao.batchConfirm(detaliList);
				//保存日志
				wmsPurchaseOrderHistoryService.saveOutWarehouseLog(detaliList, WmsOrderLogStatus.CONFIRM.getOperation());
				
				// 更新库存
				wmsWarehouseStorageService.editOutWarehouseStorage(detaliList);
			}
		return resultBean;
	}
	@Override
	public ResultBean getOrderOutType(WmsOutWarehouseOrder wmsInWarehouseOrder) {
		ResultBean resultBean = new ResultBean(true);
		try{
			List<String> types = wmsOutWarehouseOrderDao.getOrderOutType(wmsInWarehouseOrder);
			if(types!=null && types.size()==1){
				resultBean.setObject(types.get(0));
			}
		}catch(Exception e){
			e.printStackTrace();
			resultBean.setSuccess(false);
			resultBean.setMessage(e.getMessage());
		}
		return resultBean;
	}
	
	public List<WmsOutWarehouseOrder> queryAllOrderByNoOrId(WmsOutWarehouseOrder wmsOutWarehouseOrder){
		return wmsOutWarehouseOrderDao.queryAllOrderByNoOrId(wmsOutWarehouseOrder);
	}
}
